<?php

class IndexController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->req = $this->getRequest();
    }

    /**
     * 紫燕燃动世界杯
     * 入口首页
     */
    public function indexAction()
    {
        $this->_helper->layout()->disableLayout();

        $redirect_url = $_SERVER['REQUEST_URI'] ?? '';
        $redirect_query_string = isset($_SERVER['REDIRECT_QUERY_STRING']) ? '?'.$_SERVER['REDIRECT_QUERY_STRING'] : '';
        $original_url = 'http://'.\Util\Config::get('weixin_domain','Env').$redirect_url.$redirect_query_string;
        $share_url = 'http://'.\Util\Config::get('weixin_domain','Env');
        $app_id = \Util\Config::get('weixin_appid','Env');
        $app_secret = \Util\Config::get('weixin_appsecret','Env');
        $jsapi = new \Jsapipay();
        $result = $jsapi->getSignature($original_url,$app_id,$app_secret);
        $result = $this->object2array($result);
        $result['url'] = $share_url;
        $result['title'] = $this->getShareTitle();
        \Util\LogUtil::log('share','result:'.json_encode($result,true));
        $this->view->result = $result;

        //根据当前时间获取客户排行榜中奖信息
        $this->view->res = $this->getUserWinning();
        $this->view->title = '紫燕燃动世界杯';
    }
    private function getShareTitle(){
        $shareList = array(
            '紫燕邀您玩转俄罗斯！',
            '参与竞猜赢积分，把iPhone X装进口袋！',
            'You can fly！参与竞猜，和我一起飞去俄罗斯吧！',
            '我离“俄罗斯七日游”的大奖只有一步之遥了！',
            '猜球赛赢出国游，还有iphoneX等你拿，分享获得更多机会！',
            '猜球免费送你去俄罗斯，还有iphoneX等上万豪礼等你拿！'
        );
        $em = array_rand($shareList);
        return $shareList[$em];
    }

    /**
     * @return int
     * 获取用户实物奖品ID，根据ID弹出不同的图片
     */
    private function getUserWinning(){
        $time = date('Y-m-d',time());
        $type = '';
        $week_type = '';
        switch ($time) {
            case '2018-06-21'://第一周
                $type = 1;
                $week_type = 1;
                break;
            case '2018-06-28'://第二周
                $type = 1;
                $week_type = 2;
                break;
            case '2018-07-05'://第三周
                $type = 1;
                $week_type = 3;
                break;
            case '2018-07-11'://第四周
                $type = 1;
                $week_type = 4;
                break;
            case '2018-07-16'://总排行
                $type = 2;
                $week_type = 0;
                break;
        }
        $rest = new \Base\Rest();
        $req['uid'] = $this->user_id;
        $req['type'] = $type;
        $req['week_type'] = $week_type;
        $res = $rest->get('MyProduct','', $req);
        $tmp = array(
            'rank_id'=>0,
            'pid'=>0

        );
        if ($res->isSuccess()) {
            $productList = $res->getResponse();
            if(!empty($productList)){
                if($productList[0]['read'] == 0){
                    $tmp['rank_id'] = $productList[0]['id'];
                    $tmp['pid'] =$productList[0]['pid'];
                }
            }
        }
        return $tmp;
    }

    public function updateUserRankAction(){
        $param = $this->req->getPost();
        $req['uid'] = $this->user_id;
        $postData = array(
            'id'=>$param['id'],
            'read'=>1,
        );
        $post = array('payload' => base64_encode(http_build_query($postData)));
        $rest = new \Base\Rest();
        $res = $rest->put('MyProduct', $param['id'],$req, $post);
        if ($res->isSuccess()) {
            $re = $res->getResponse();
            Response::Success($re);
        } else {
            Response::Error(201, $res->getErrorMessage());
        }

    }

    public function homeAction()
    {
        $this->view->title = '紫燕燃动世界杯';
    }
    public function musicAction()
    {
        $this->view->title = '紫燕燃动世界杯';
    }
    public function getAwardAction(){
        $reset = new \Base\Rest();
        $req = array('uid' => $this->user_id);
        $rest_res = $reset->get('Award', false, $req);
        if ($rest_res->isSuccess()) {
            $award_res = $rest_res->getResponse();
            return Response::Success($award_res);
        } else {
            return Response::Error("202",$rest_res->getErrorMessage());
        }
    }
    public function getIsClickWinningAction()
    {
        $reset = new \Base\Rest();
        $req = array('uid' => $this->user_id);
        $rest_res = $reset->get('IsClickWinning', false, $req);
        if ($rest_res->isSuccess()) {
            $award_res = $rest_res->getResponse();
            return Response::Success($award_res);
        } else {

            return Response::Error("202",$rest_res->getErrorMessage());
        }
    }
}